package com.mei.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.mei.demo.domain.City;
import com.mei.demo.domain.CitySearcher;
import com.mei.demo.response.ResponseUtil;
import com.mei.demo.service.CityService;

/**
 * @author mei.xiaoliang@qq.com
 */
@RestController
@RequestMapping("/city")
public class CityController extends BaseController {

    @Autowired
    private CityService cityService;

    @GetMapping("list")
    public ModelAndView searchPage() {

        ModelAndView mov = new ModelAndView();
        mov.setViewName("city/list");

        return mov;
    }

    @PostMapping("searchpageajax")
    public String searchPageAjax(CitySearcher searcher) {

        //        List<City> list = cityService.searchPage(searcher);

        return ResponseUtil.buildSuccessResponse();
    }

    @RequestMapping(value = "/browse/{id}", method = { RequestMethod.GET })
    public ModelAndView browse(@PathVariable Long id) {

        City city = cityService.browse(id);

        ModelAndView mov = new ModelAndView();
        mov.setViewName("city/browse");
        mov.addObject("city", city);

        return mov;
    }

    @RequestMapping(value = "/add", method = { RequestMethod.GET })
    public ModelAndView add() {

        ModelAndView mov = new ModelAndView();
        mov.setViewName("city/add");

        return mov;
    }

    @RequestMapping(value = "/add/post", method = { RequestMethod.POST })
    public ModelAndView add(City city) {

        cityService.insert(city);

        ModelAndView mov = new ModelAndView("redirect:/city/list");

        return mov;
    }

    @RequestMapping(value = "/edit/{id}", method = { RequestMethod.GET })
    public ModelAndView edit(@PathVariable Long id) {

        City city = cityService.browse(id);

        ModelAndView mov = new ModelAndView();
        mov.setViewName("city/edit");
        mov.addObject("city", city);
        mov.addObject("", "sdf");
        return mov;
    }

    @RequestMapping(value = "/edit/post", method = { RequestMethod.POST })
    public ModelAndView editPost(City city) {

        cityService.update(city);

        ModelAndView mov = new ModelAndView("redirect:/city/list"); // 默认为forward模式

        return mov;
    }

    @RequestMapping(value = "/delete/{id}", method = { RequestMethod.GET, RequestMethod.POST })
    public String del(@PathVariable Long id) {

        String result = ResponseUtil.buildSuccessResponse();

        try {
            cityService.delete(id);
        } catch (Exception e) {
            // TODO: handle exception
        }

        return result;
    }

}